package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.constant.RabbitConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.api.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 封捷
 * @create-date: 2022/8/23 10:08
 */
@Api("SKU管理接口")
@RestController
@RequestMapping("/admin/product")
public class SkuManagerController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private RabbitService rabbitService;

    /**
     * 根据 spuId 查询 SPU 图片集合
     * 因为这个功能是创建 SKU 的准备工作所以放在这里
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(
            @PathVariable("spuId") Long spuId) {

        // 调用 Service 方法查询数据
        List<SpuImage> spuImageList = managerService.getSpuImageList(spuId);

        return Result.ok(spuImageList);
    }

    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {

        List<SpuSaleAttr> spuSaleAttrList = managerService.getSpuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrList);
    }

    @PostMapping("/saveSkuInfo")
    public Result<Void> saveSkuInfo(@RequestBody SkuInfo skuInfo) {

        managerService.saveSkuInfo(skuInfo);

        // 附加功能：通过消息队列，对接 ElasticSearch 中商品上架功能
        // 1、获取 skuId
        Long skuId = skuInfo.getId();

        // 2、发送消息
        rabbitService.sendMessage(RabbitConst.RABBIT_EXCHANGE_GOODS, RabbitConst.RABBIT_ROUTING_KEY_GOODS_IMPORT, skuId);

        return Result.ok();
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result<IPage<SkuInfo>> getSkuInfoPage(
            @PathVariable("pageNum") Long pageNum,
            @PathVariable("pageSize") Long pageSize) {

        IPage<SkuInfo> skuInfoIPage = managerService.getSkuInfoPage(pageNum, pageSize);

        return Result.ok(skuInfoIPage);
    }

    @GetMapping("/onSale/{skuId}")
    public Result<Void> onSale(@PathVariable("skuId") Long skuId) {

        int isSale = 1;

        managerService.updateSkuSaleStatus(skuId, isSale);

        // 发送消息：执行 ElasticSearch 数据装入
        rabbitService.sendMessage(RabbitConst.RABBIT_EXCHANGE_GOODS, RabbitConst.RABBIT_ROUTING_KEY_GOODS_IMPORT, skuId);

        return Result.ok();
    }

    @GetMapping("/cancelSale/{skuId}")
    public Result<Void> cancelSale(@PathVariable("skuId") Long skuId) {

        int isSale = 0;

        managerService.updateSkuSaleStatus(skuId, isSale);

        // 发送消息：执行 ElasticSearch 数据删除
        rabbitService.sendMessage(RabbitConst.RABBIT_EXCHANGE_GOODS, RabbitConst.RABBIT_ROUTING_KEY_GOODS_REMOVE, skuId);

        return Result.ok();
    }
}
